package com.zy.kotlinbaselibrary.widget.toolbar

import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.app.ActionBar
import android.support.v7.widget.Toolbar
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import kotlinx.android.synthetic.main.toolbar.view.*

class CustomToolbar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0):
    Toolbar(context, attrs, defStyleAttr) {

    /**
     * 设置标题内容
     */
    fun setToolbarTitleContent(title: String){
        this.title = ""
        toolbar_title.visibility = View.VISIBLE;
        toolbar_title.text = title
    }

    /**
     * 设置标题颜色
     */
    fun setToolbarTitleColor(color: Int) {
        toolbar_title.setTextColor(color)
    }

    /**
     * 设置返回文字
     * @param text
     */
    fun setToolbarBackText(text: String) {
        toolbar_back.text = text
        toolbar_back.visibility = View.VISIBLE
    }

    /**
     * 设置返回文字颜色
     *
     */
    fun setToolbarBackTColor(color: Int) {
        toolbar_back.setTextColor(color)
    }


    //设置返回图标
    fun setToolbarBackDrawable(res: Int) {
        if (-1 == res) {
            toolbar_back.setCompoundDrawables(null, null, null, null)
            return
        }

        val dwBack = ContextCompat.getDrawable(context, res)
        dwBack?.let {
            it.setBounds(0, 0, it.minimumWidth, it.minimumHeight)
        }

        toolbar_back.setCompoundDrawables(dwBack, null, null, null)
    }

    /**
     * 返回点击事件
     * @param clickListener
     */
    fun setToolbarBackOnClick(clickListener: View.OnClickListener) {
        toolbar_back.setOnClickListener(clickListener)
    }

    /**
     * 设置确定文字
     * @param text
     */
    fun setToolbarConfirmText(text: String?) {

        if (TextUtils.isEmpty(text)) {
            toolbar_confirm.visibility = View.INVISIBLE
        } else {
            toolbar_confirm.text = text
            toolbar_confirm.visibility = View.VISIBLE
        }

    }

    /**
     * 设置确定文字颜色
     *
     */
    fun setToolbarConfirmTColor(color: Int) {
        toolbar_confirm.setTextColor(color)
    }

    //设置确定图标
    fun setToolbarConfirmDrawable(res: Int) {
        val dwConfirm = ContextCompat.getDrawable(context, res)
        dwConfirm?. let {
            it.setBounds(0, 0, it.minimumWidth, it.minimumHeight)
        }
        toolbar_confirm.setCompoundDrawables(null, null, dwConfirm, null)
    }

    /**
     * 确定点击事件
     * @param clickListener
     */
    fun setToolbarConfirmOnClick(clickListener: View.OnClickListener) {
        toolbar_confirm.setOnClickListener(clickListener)
    }

    fun isUseDefaultBack(actionBar: ActionBar, isShow: Boolean) {
        actionBar.setDisplayHomeAsUpEnabled(isShow)  // 给左上角图标的左边加上一个返回的图标
        actionBar.setDisplayShowHomeEnabled(isShow)
    }


    fun setGone(view: Int) {
        findViewById<View>(view).visibility = View.GONE
    }

    fun getToolbarConfirmText(): String = toolbar_confirm.text.toString().trim()
}